<template>
  <div class="app-main">
    <el-card shadow="never">
      <el-row>
        <el-form :inline="true" :model="queryForm" ref="queryForm">
          <el-form-item label="姓名" prop="nickname">
            <el-input v-model="queryForm.nickname" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="queryData">查询</el-button>
            <el-button @click="resetForm('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-button type="primary" plain icon="el-icon-plus" @click="addShow">新增</el-button>
      </el-row>
      <el-row class="table-container">
        <el-table :data="tableData" v-loading="loading" element-loading-text="数据加载中···" stripe style="width: 100%" size="medium" max-height="630">
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="姓名" prop="nickname" show-overflow-tooltip></el-table-column>
          <el-table-column label="账号" prop="username" show-overflow-tooltip></el-table-column>
          <el-table-column label="性别" prop="sex" :formatter="sexRender" show-overflow-tooltip></el-table-column>
          <el-table-column label="电话" prop="phone" show-overflow-tooltip></el-table-column>
          <el-table-column label="创建日期" prop="createTime" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" class="el-icon-edit" @click="updateShow(scope.row)">修改</el-button>
              <el-button type="text" class="el-icon-delete" @click="deleteShow(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.pageNo"
          :page-sizes="page.pageSizes"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </el-row>
    </el-card>
    <el-dialog :title="formTitle" :visible.sync="addFormVisible" width="600px" :close-on-click-modal="closeModal">
      <el-form :model="addForm" :rules="rules" ref="addForm" label-width="70px">
        <el-form-item label="姓名" prop="nickname">
          <el-input v-model="addForm.nickname" placeholder="请输入姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input v-model="addForm.username" placeholder="请输入账号" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" placeholder="请输入密码" show-password clearable></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="addForm.sex" placeholder="请选择性别" clearable style="width: 100%">
            <el-option v-for="item in genderArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addForm.phone" placeholder="请输入电话" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelForm('addForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addUser, deleteUser, editUser, getUser} from "@/api/user";

export default {
  name: "index",
  data() {
    return {
      genderArr: [{'label': '男', 'value': 1}, {'label': '女', 'value': 2}],
      queryForm: {
        nickname: ''
      },
      loading: false,
      tableData: [],
      page: {
        pageNo: 1,
        pageSizes: [10, 50, 100, 200, 500],
        pageSize: 10,
        total: 0
      },
      addFormVisible: false,
      formTitle: '新增',
      addForm: {
        id: '',
        nickname: '',
        username: '',
        password: '',
        sex: '',
        phone: ''
      },
      rules: {
        nickname: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
        ],
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'change'},
        ],
        phone: [
          {required: true, message: '请输入电话', trigger: 'blur'},
        ],
      },
      closeModal: false
    }
  },
  created() {
    this.queryData()
  },
  methods: {
    queryData() {
      this.loading = true
      let params = {
        nickname: this.queryForm.nickname,
        role: "admin",
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize
      }
      getUser(params).then(response => {
        if(response.code === 200) {
          this.tableData = response.data.list
          this.page.total = response.data.total
        } else {
          this.$message.error(response.msg);
        }
        this.loading = false
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.queryData()
    },
    sexRender(data) {
      if(data.sex === 1) {
        return '男'
      } else if(data.sex === 2) {
        return '女'
      } else {
        return '未知'
      }
    },
    addShow() {
      this.formTitle = '新增'
      this.addForm.id = ''
      this.addForm.nickname = ''
      this.addForm.username = ''
      this.addForm.password = ''
      this.addForm.sex = ''
      this.addForm.phone = ''
      this.addFormVisible = true
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.addForm.id !== '') {
            let params = {
              id: this.addForm.id,
              nickname: this.addForm.nickname,
              username: this.addForm.username,
              sex: this.addForm.sex,
              phone: this.addForm.phone,
              role: 'admin'
            }
            editUser(params).then(response => {
              if(response.code === 200){
                this.$message({
                  message: response.msg,
                  type: 'success'
                });
              } else {
                this.$message({
                  message: response.msg,
                  type: 'error'
                });
              }
              this.addFormVisible = false
              this.queryData()
            })
          } else {
            let params = {
              nickname: this.addForm.nickname,
              username: this.addForm.username,
              sex: this.addForm.sex,
              phone: this.addForm.phone,
              password: this.addForm.password,
              role: 'admin'
            }
            addUser(params).then(response => {
              if(response.code === 200){
                this.$message({
                  message: response.msg,
                  type: 'success'
                });
              } else {
                this.$message({
                  message: response.msg,
                  type: 'error'
                });
              }
              this.addFormVisible = false
              this.queryData()
            })
          }
        } else {
          return false;
        }
      });
    },
    cancelForm(formName) {
      this.$refs[formName].resetFields();
      this.addFormVisible = false
    },
    updateShow(row) {
      this.formTitle = '修改'
      this.addForm.id = row.id
      this.addForm.nickname = row.nickname
      this.addForm.username = row.username
      this.addForm.password = row.password
      this.addForm.sex = row.sex
      this.addForm.phone = row.phone
      this.addFormVisible = true
    },
    deleteShow(row) {
      this.$confirm('是否确认删除选中的数据项？').then(function() {
        return deleteUser(row.id)
      }).then((response) => {
        if(response.code === 200){
          this.$message({
            message: response.msg,
            type: 'success'
          });
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          });
        }
        this.queryData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      })
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.page.pageNo = 1
      this.queryData()
    },
    handleCurrentChange(val) {
      this.page.pageNo = val
      this.queryData()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-main {
  padding: 20px;
  box-sizing: border-box;

  .table-container {
    margin-top: 20px;
  }

  .pagination-container {
    margin: 20px 0;
    float: right;
  }
}
</style>
